package com.tust.service;

import com.tust.bean.Course;
import com.tust.dao.CourseDao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class CourseServiceImpl implements BaseService{

    // 课程表业务逻辑
    public  void service(Connection connection) {

        Scanner scanner = new Scanner(System.in);
        String type;
        List<Course> list;

        while (true) {
            System.out.println("请输入课程管理操作命令：add->添加，edit->编辑,del->删除,Query->查询，QUIT->退出");
            CourseDao courseDao = new CourseDao(connection);

            type = scanner.next();
            if (type.equalsIgnoreCase("add")) {
                System.out.println("请依次单独输入课程号、课程名、学分：");
                Course course = new Course();
                course.setCid(scanner.next());
                course.setCname(scanner.next());
                course.setCcredit(scanner.nextDouble());
                if (courseDao.insert(course)) {
                    System.out.println("添加成功");
                    list = courseDao.query();
                    print(list);

                } else {
                    System.out.println("添加失败");
                }
            } else if (type.equalsIgnoreCase("del")) {
                // 打印课程信息
                list = courseDao.query();
                print(list);

                System.out.println("删除指定课程请输入课程号，删除所有课程输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    if (courseDao.delete()) {
                        System.out.println("所有课程记录删除成功！");
                    } else {
                        System.out.println("所有课程记录删除失败！");
                    }
                } else {
                    if (courseDao.delete(type.trim())) {
                        System.out.println(type + "课程删除成功！");
                        list = courseDao.query();
                        print(list);
                    } else {
                        System.out.println(type + "课程删除失败！");
                    }
                }
            } else if (type.equalsIgnoreCase("edit")) {
                list = courseDao.query();
                print(list);
                System.out.println("请依次单独输入要更新课程的课程号、课程名、学分：");
                Course course = new Course();
                course.setCid(scanner.next());
                course.setCname(scanner.next());
                course.setCcredit(scanner.nextDouble());
                if (courseDao.update(course)) {
                    System.out.println("更新成功");
                    list = courseDao.query();
                    print(list);
                } else {
                    System.out.println("更新失败");
                }
            } else if (type.equalsIgnoreCase("query")) {
                System.out.println("查询指定课程请输入课程号，查询所有课程输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    list = courseDao.query();
                    print(list);
                } else {
                    Course c = courseDao.query(type);
                    if (c != null) {
                        System.out.println(c.getCid() + "\t" + c.getCname() + "\t" + c.getCcredit());
                    } else { System.out.println("查询的课程号:" + type + "不存在！"); }
                }
            } else if (type.equalsIgnoreCase("QUIT")) {
                courseDao.close();
                break;
            }
        }
    }

    // 打印课程表信息
    @Override
    public void print(List list) {

        if(list.size()>0) {
            Iterator<Course> iterator = list.iterator();
            System.out.println("课程号\t课程名\t\t\t  学分");
            while (iterator.hasNext()) {
                Course course = iterator.next();
                String cid = String.format("%-8s", course.getCid());
                String cname = String.format("%-16s", course.getCname());
                String ccredit = String.format("%-4s", course.getCcredit());
                System.out.println(cid + cname + ccredit);
            }
        }else{
            System.out.println("无课程记录，请录入！");
        }
    }
}
